package com.itheima.app.controller;

import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/27
 */
@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    //查询小视频列表
    @GetMapping("/smallVideos")
    public PageBeanVo findSmallVideos(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return videoManager.findSmallVideos(page, pageSize);
    }

    //上传小视频
    @PostMapping("/smallVideos")
    public void saveSmallVideos(
            MultipartFile videoThumbnail,  //封面
            MultipartFile videoFile // 视频
    ) throws IOException {
        videoManager.saveSmallVideos(videoThumbnail, videoFile);
    }

    //关注
    @PostMapping("/smallVideos/{videoUserId}/userFocus")
    public void userFocus(
            @PathVariable Long videoUserId
    ) {
        videoManager.saveUserFocus(videoUserId);
    }

    //取消关注
    @PostMapping("/smallVideos/{videoUserId}/userUnFocus")
    public void userUnFocus(
            @PathVariable Long videoUserId
    ) {
        videoManager.removeUserUnFocus(videoUserId);
    }

    //视频点赞
    @PostMapping("/smallVideos/{videoId}/like")
    public void like(
            @PathVariable String videoId
    ) {
        videoManager.saveLike(videoId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{videoId}/dislike")
    public void dislike(
            @PathVariable String videoId
    ) {
        videoManager.removeDisLike(videoId);
    }

    //发布评论
    @PostMapping("/smallVideos/{videoId}/comments")
    public void saveComment(
            @PathVariable String videoId,
            @RequestBody Map map
    ) {
        String content = map.get("comment").toString();
        videoManager.saveVideoComment(videoId, content);
    }

    //查询评论列表
    @GetMapping("/smallVideos/{videoId}/comments")
    public PageBeanVo findCommentList(
            @PathVariable String videoId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return videoManager.findCommentList(videoId, page, pageSize);
    }

    //评论点赞
    @PostMapping("/smallVideos/comments/{commentId}/like")
    public void commentLike(
            @PathVariable String commentId
    ) {
        videoManager.commentLike(commentId);
    }

    //取消评论点赞
    @PostMapping("/smallVideos/comments/{commentId}/dislike")
    public void commentDislike(
            @PathVariable String commentId
    ) {
        videoManager.commentDislike(commentId);
    }
}
